package com.cdtu.labpreview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.common.UserHolder;
import com.cdtu.labpreview.controller.vo.BaseIntroductionVo;
import com.cdtu.labpreview.entity.*;
import com.cdtu.labpreview.entity.Class;
import com.cdtu.labpreview.mapper.*;
import com.cdtu.labpreview.service.IBaseIntroductionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseIntroductionServiceImpl extends ServiceImpl<BaseIntroductionMapper, BaseIntroduction> implements IBaseIntroductionService {

    @Autowired
    private BaseIntroductionMapper baseIntroductionMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ClassMapper classMapper;


    @Override
    public Result getBaseIntroductionOfTeacher(int tno) {
        LambdaQueryWrapper<BaseIntroduction> lqw=new LambdaQueryWrapper<>();
        lqw.eq(BaseIntroduction::getTno,tno);
        List<BaseIntroduction> baseIntroductions = baseIntroductionMapper.selectList(lqw);

        List<BaseIntroductionVo> introductionVoList = baseIntroductions.stream().map(baseIntroduction -> {
            BaseIntroductionVo biv = new BaseIntroductionVo();
            BeanUtils.copyProperties(baseIntroduction, biv);
            //获取教师名
//            LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            int tno = baseIntroduction.getTno();
//            lambdaQueryWrapper.eq(Teacher::getTno, tno);
//            Teacher teacher = teacherMapper.selectOne(lambdaQueryWrapper);
//            String name = teacher.getName();
//            biv.setTeacher(name);
            //获取班级名
            long cid = baseIntroduction.getClassId();
            Class aClass = classMapper.selectById(cid);
            String cname = aClass.getName();
            biv.setClassName(cname);
            return biv;
        }).collect(Collectors.toList());
        return Result.ok(introductionVoList);
    }

    @Override
    public Result getBaseIntroductionOfStudent(int sno) {
        //根据学号获取班级id
        LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Student::getSno,sno);
        Student student = studentMapper.selectOne(lqw);
        long classId = student.getClassId();
        //根据班级id获取对应老师发布的基本介绍
        LambdaQueryWrapper<BaseIntroduction> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(BaseIntroduction::getClassId,classId);
        List<BaseIntroduction> baseIntroductions = baseIntroductionMapper.selectList(lqw2);


        List<BaseIntroductionVo> introductionVoList = baseIntroductions.stream().map(baseIntroduction -> {
            BaseIntroductionVo biv = new BaseIntroductionVo();
            BeanUtils.copyProperties(baseIntroduction, biv);
            //获取教师名
            LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            int tno = baseIntroduction.getTno();
            lambdaQueryWrapper.eq(Teacher::getTno, tno);
            Teacher teacher = teacherMapper.selectOne(lambdaQueryWrapper);
            String name = teacher.getName();
            biv.setTeacher(name);
            //获取班级名
            long cid = baseIntroduction.getClassId();
            Class aClass = classMapper.selectById(cid);
            String cname = aClass.getName();
            biv.setClassName(cname);
            return biv;
        }).collect(Collectors.toList());
        return Result.ok(introductionVoList);
    }

    @Override
    public Result addBaseIntroduction(BaseIntroduction baseIntroduction) {
        int userNo = UserHolder.getUser().getUserNo();
        baseIntroduction.setTno(userNo);
        int flag = baseIntroductionMapper.insert(baseIntroduction);
        if(flag<=0) return Result.fail("添加失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result updateBaseIntroduction(BaseIntroduction baseIntroduction) {
        int flag = baseIntroductionMapper.updateById(baseIntroduction);
        if(flag<=0) return Result.fail("修改失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result deleteBaseIntroduction(long id) {
        int flag = baseIntroductionMapper.deleteById(id);
        if(flag<=0) return Result.fail("删除失败！请联系管理员");
        return Result.ok();
    }
}
